<?php
namespace Index\Controller;
use Think\Controller;
class ApiController extends Controller {
    public function index(){

   
    
    }


    public function loginAction() {
      $count = I("post.usercount","","strip_tags");
      $pwd = I("post.password","","strip_tags");
      $verify = I("post.verifycode");

      if (!check_verify($verify)) {
          $data["data"] = [];
          $data["rt"] = 10001;
          $data["msg"] = "验证码错误";
          return $this->ajaxReturn($data);
       } 

       if ($count == ""||$pwd == "") {
          $data["rt"] = 10002;
          $data["msg"] = "用户名或密码不能为空!";
          return $this->ajaxReturn($data);
       }
 
       $model = M("userinfo");
       $where["email"] = $count;
       $userinfo = $model->where($where)->find();
       $userId = $userinfo["id"];       
       if (!isset($userId)|| ($userinfo["password"] != $pwd)) {
          $data["rt"] = 10002;
          $data["msg"] = "用户名或密码错误 ";
          return $this->ajaxReturn($data);
       }

       $data["rt"] = 1;
       $data["msg"] = "登陆成功"; 

       session("uinfo",$userinfo);   
       return $this->ajaxReturn($data);
    }

    public function registAction() {
      $count = I("post.usercount","","strip_tags");
      $pwd = I("post.password","");
      $verify = I("post.verifycode");

      if (!check_verify($verify)) {
          $data["data"] = [];
          $data["rt"] = 10001;
          $data["msg"] = "验证码错误";
          return $this->ajaxReturn($data);
       } 

       $model = M("userinfo");
       $where["email"] = $count;
       $userinfo = $model->where($where)->find();
       if (isset($userinfo)) {
          $data["rt"] = 100048;
          $data["msg"] = "该用户已经注册";
          return $this->ajaxReturn($data);
       }

       $info["email"] = $count;
       $info["password"] = $pwd;

        $model->add($info);
        $data["rt"] = 1;
        $data["msg"] = "注册成功!";
        return $this->ajaxReturn($data);
    }

    public function logoutAction() {
      session("uinfo",null);
      $data["rt"] = 1;
      $data["msg"] = "退出成功";
      return $this->ajaxReturn($data);
    }

    public function getChannelInfo() {
        $model = M('channel');
        $limit_count = 9;
        $limit_page = intval(I("get.page",1));
        if ($limit_page != 0) {
            $limit_page = $limit_page - 1;
        }
       $currectIndex = $limit_page * $limit_count;
       $data["data"] = $model->limit($currectIndex,$limit_count)->select();
       $data["rt"] = 1;
       $data["msg"] = "";
       $allcount = $model->query("select count(id) from __TABLE__");
       $data["all_count"] = $allcount[0]["count(*)"];
       return $this->ajaxReturn($data);
    }

    public function getMusicInfo() {

      $channel_id = I("get.channel_id",0);
      $limit_page = intval(I("get.page",1));

      $url = "http://www.app-echo.com/api/channel/info?order=hot&id=".$channel_id."&page=".$limit_page;

      $musicJson = get_json_info($url);
      $musicData = json_decode($musicJson,true);

      if (intval($musicData["status"]) != 100) {
        $data["rt"] = $musicData["status"];
        $data["msg"] = "请求失败";
        $data["data"] = [];
        return $this->ajaxReturn($data);
      }
        $data["rt"] = 1;
        $data["msg"] = "请求成功";
        $data["data"] = $musicData["sounds"];
        return $this->ajaxReturn($data);
    }

    public function getSoundInfo() {
      $sound_id = I("get.id",0);

      $url = "http://www.app-echo.com/api/sound/info?id=".$sound_id;
      $musicJson = get_json_info($url);
      $musicData = json_decode($musicJson,true);

       if (intval($musicData["status"]) != 100) {
          $data["rt"] = $musicData["status"];
          $data["msg"] = "请求失败";
          $data["data"] = [];
          return $this->ajaxReturn($data);
       }
        $data["rt"] = 1;
        $data["msg"] = "请求成功";
        $data["data"] = $musicData["info"];
        return $this->ajaxReturn($data);
    }
   
   




}